package com.wen.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wen.service.CartService;

import com.wen.common.R;
import com.wen.pojo.Cart;
import com.wen.pojo.dto.ProductAndCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xie
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 购物车查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getProductAndCart/{id}")
    public R getProductAndCart(@PathVariable String id) {
        List<ProductAndCart> productAndCart = cartService.getProductAndCart(id);

        return R.ok().data("productAndCart", productAndCart);
    }

    /**
     * 查询该用户的购物车的数量
     *
     * @param id
     * @return
     */
    @GetMapping("/ProductAndCartCount/{id}")
    public R ProductAndCartCount(@PathVariable String id) {

        int productAndCartCount = cartService.getProductAndCartCount(id);
        return R.ok().data("count", productAndCartCount);
    }


    /**
     * 查看用户选中的商品
     *
     * @param id
     * @return
     */
    @GetMapping("/getProductAndCartSelected/{id}")
    public R getProductAndCartSelected(@PathVariable String id) {
        List<ProductAndCart> productAndCartSelected = cartService.getProductAndCartSelected(id);

        return R.ok().data("productAndCartSelected", productAndCartSelected);
    }


    /**
     * 查总金额总件数
     *
     * @param id
     * @return
     */
    @GetMapping("/getProductAndCartSum/{id}")
    public R getProductAndCartSum(@PathVariable String id) {
        ProductAndCart productAndCartSum = cartService.getProductAndCartSum(id);

        return R.ok().data("productAndCartSum", productAndCartSum);
    }


    /**
     * 根据商品删除对应的购物车数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/productAndCartDelete/{id}")
    public R removeProductAndCart(@PathVariable String id) {

        HashMap<String, Object> map = new HashMap<>();

        map.put("productId", id);
        boolean flag = cartService.removeByMap(map);
        if (flag) {
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");

    }

    /**
     * 购物车添加
     *
     * @param cart
     * @return
     */
    @PostMapping("/CartAdd")
    public R addProduct(@RequestBody Cart cart) {
        boolean flag = cartService.save(cart);
        if (flag) {
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }

    /**
     * 购物车点击count修改
     *
     * @param count
     * @param pid
     * @return
     */
    @PutMapping("/UpdateCount/{count}/{pid}")
    public R UpdateCount(@PathVariable String count,
                         @PathVariable Integer pid) {
        int updateCount = cartService.getUpdateCount(count, pid);

        if (updateCount > 0) {
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败！");
    }


    /**
     * 购物车客户是否选中商品
     *
     * @param id
     * @param sed
     * @return
     */
    @PutMapping("/UpdateSelected/{id}/{sed}")
    public R UpdateSelected(@PathVariable String id,
                            @PathVariable Integer sed) {
        int updateSelected = cartService.getUpdateSelected(id, sed);

        if (updateSelected > 0) {
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败！");
    }


    /**
     * 购物车批量删除
     *
     * @param
     * @return
     */

    @PostMapping("/del")
    public boolean del(@RequestBody List<String> ids) {
        return cartService.removeByIds(ids);
    }

    @PostMapping("/delById/{id}")
    public R delById(@PathVariable Integer id) {

        //删除条件的封装map
        Map<String, Object> map = new HashMap<>();
        map.put("consumer_id", id);
        map.put("selected",1);
        //执行删除
        boolean result = this.cartService.removeByMap(map);
        if (result) {
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");
    }


}
